package java_class_work.week08.internet.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @Author Tiam
 * @Date 2022/11/1 10:50
 * @Description:
 */
public class TcpClient02 {
    public static void main(String[] args) {
        try {
            // 发送消息
            Socket socket = new Socket("127.0.0.1", 8888);
            byte[] bytes = "服务端收到请回答,over".getBytes();
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(bytes);
            // 接受服务端返回的消息
            InputStream inputStream = socket.getInputStream();
            byte[] bytes1 = new byte[1024];
            // 阻塞式方法read, 等待接收返回的消息.
            int len = inputStream.read(bytes1);
            System.out.println(new String(bytes1, 0, len));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
